package com.sms.plateform.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;

import com.sms.plateform.common.finals.Constants;
import com.sms.plateform.common.util.Utils;
import com.sms.plateform.kernel.entity.UserInfo;
@Controller
public class BaseController {
	protected static final Log log = LogFactory.getLog("log");
	/**
	 * 获取用户信息
	 * @param request
	 * @return
	 */
	protected UserInfo getUserInfo(HttpServletRequest request) {
		UserInfo userInfo = (UserInfo) request.getSession(true).getAttribute(Constants.SESSION_USER);
		if (userInfo == null || userInfo.getUsername().equals("")) {
			return null;
		}
		return userInfo;
	}
	protected void setSessionObject(HttpServletRequest request,String key,Object obj){
		request.getSession().setAttribute(key, obj);
	}
	protected Object getSessionObject(HttpServletRequest request,String key){
		return request.getSession().getAttribute(key);
	}
	/**
	 * 获取页面传来的参数，为空则为默认值
	 */
	protected String getUrlParam(HttpServletRequest request, String name,String defValue) {
		if(request.getParameter(name)==null||StringUtils.isEmpty(request.getParameter(name))){
			return defValue;
		}else{
			return Utils.trim(request.getParameter(name));
		}
	}
	/**
	 * 获取页面传来的所有参数
	 */
	protected String getUrlParam(HttpServletRequest request, String name) {
		return Utils.trim(request.getParameter(name));
	}
	private static byte[] lock = new byte[0];
	protected final void setNoCache(HttpServletRequest request, HttpServletResponse response) {
		synchronized (lock) {
			if (request.getProtocol().compareTo("HTTP/1.0") == 0)
				response.setHeader("Prama", "no-cache");
			else if (request.getProtocol().compareTo("HTTP/1.1") == 0)
				response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
		}
	}
	
}
